package com.gzhu.knowledgeAdmin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzhu.knowledgeAdmin.common.constant.CheckResultType;
import com.gzhu.knowledgeAdmin.common.utils.UserInfoUtils;
import com.gzhu.knowledgeAdmin.common.vo.PageVo;
import com.gzhu.knowledgeAdmin.entity.Rule;
import com.gzhu.knowledgeAdmin.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.gzhu.knowledgeAdmin.dao.CheckResultDao;
import com.gzhu.knowledgeAdmin.entity.CheckResult;
import com.gzhu.knowledgeAdmin.service.CheckResultService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("checkResultService")
public class CheckResultServiceImpl extends ServiceImpl<CheckResultDao, CheckResult> implements CheckResultService {

    @Autowired
    private RuleService ruleService;

    @Override
    public PageVo queryPage(Map<String, Object> params) {
        // 获取当前页和每页大小
        long pageNo = Long.parseLong((String) params.get("page"));
        long pageSize = Long.parseLong((String) params.get("limit"));
        String statusStr = (String) params.get("status");

        String keyWord = (String) params.get("keyWord");

        Page<CheckResult> page = new Page<>(pageNo, pageSize);
        QueryWrapper<CheckResult> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(keyWord)){
            wrapper.like("r.F_RWNR",keyWord);
        }
        if(!StringUtils.isEmpty(statusStr)){
            // 复核状态
            wrapper.eq("c.status",Integer.parseInt(statusStr));
        }

            // 只能查询自己公司的
        String gsid = UserInfoUtils.getCurrentUser().getGsid();
        wrapper.eq("c.gsid", gsid);


        // 根据创建时间降序排序
        wrapper.orderByDesc("c.gmt_create");

        page = baseMapper.page(page, wrapper);


        // 查询每条检测用到的规则列表
        List<CheckResult> records = page.getRecords();
        for (CheckResult record : records) {
            String ruleIds = record.getRuleIds();

            String[] split = ruleIds.split("\\|");
            List<Integer> ruleIdList = new ArrayList<>();
            for (String s : split) {
                ruleIdList.add(Integer.parseInt(s));
            }

            List<Rule> rules = (List<Rule>) ruleService.listByIds(ruleIdList);
            record.setRuleList(rules);

        }

        return new PageVo(page);
    }

    @Override
    public List<CheckResult> getRecordByGsid(String gsid) {

        QueryWrapper<CheckResult> wrapper = new QueryWrapper<>();
        wrapper.eq("gsid", gsid);

        return baseMapper.selectList(wrapper);

    }

    @Override
    @Transactional
    public void reCheckBatch(List<Integer> ids) {
        for (Integer id : ids) {
            CheckResult checkResult = new CheckResult();
            checkResult.setId(id);
            checkResult.setStatus(CheckResultType.CHECKED);
            baseMapper.updateById(checkResult);
        }
    }

}